function addServiceItem() {
    var serviceId = $('#addServiceFrm #serviceId').val();
    var serviceName = $.trim($('#addServiceFrm #serviceName').val());
    if (serviceName == '' || serviceName == 0) {
        showCustomDialog(_('Please enter service Name'), 'serviceName');
        return;
    }
    
    var lcInt = locale(decimalPoint, thousandSep, 0);
    // Quantity must be > 1
    if (!validatePositiveIntegerByLocale($('#serviceQuantity'), lcInt, true, '', 'dummy')) {
        showCustomDialog(_('Quantity must be a interger > 1'), 'serviceQuantity');
        return;
    }
    
    if (!validatePositiveIntegerByLocale($('#unitPrice'), lcInt, true, '', 'dummy')) {
        showCustomDialog(_('Price must be > 0'), 'unitPrice');
        return;
    }
    
    if (serviceId == '' || serviceId == 0) {
        showServiceWarningDialog(_('Service code is empty. Continue adding service without service code?'), 'serviceCode');                
        return;
    }   
    submitAddService();
}

function showServiceWarningDialog(message, fieldId2Focus) {
	var buttons = {};				
	buttons[_('Cancel')] = function() {
		$(this).dialog('close');			
		$('#' + fieldId2Focus).focus();
	};
	buttons[_('Continue')] = function() {
		$(this).dialog('close');			
		submitAddService();
	};		
	
	generalDialog(null, _('Notice'), message, 'warn', {
		autoOpen: true,			
		bgiframe: true,
		resizable: false,
		modal: true,
		buttons: buttons,
		dialogClass: 'custom-dialog'
	}, _('Cancel'));
}

function submitAddService() {    
    $.ajax({
        type:   'POST',
        url:    '../InsurancePayment/addPaymentItemAjax?set_ajax_view',
        data:   $('form#addServiceFrm').serialize() + 
        		'&ticketId=' + $('#ticketId').val() +
        		'&itemListRender=renderItemList',                              
        dataType: 'json',
        beforeSend: function() {  
            showAjaxLoader('addServiceLoader', 'addServiceBtnDiv', true);        
        },
        complete: function() {
            showAjaxLoader('addServiceLoader', 'addServiceBtnDiv', false);         
        },
        success: function(data) {
            if (data.error.code == 'OK') {
                setDataToElements({itemList: 'serviceListDiv', paymentSummary: 'paymentSummaryDiv'}, data.result);
                resetAddServiceItemForm();
                $('#serviceCode').focus();
            }
            else {
                displayError(data.error.mess);
            }
            
        }       
    });
}

function resetAddServiceItemForm() {
    $('#addServiceFrm #serviceCode').val('');
    $('#addServiceFrm #serviceName').val('');
    $('#addServiceFrm #serviceId').val('');
    
    $('#addServiceFrm #serviceQuantity').val(1);
    $('#addServiceFrm #unitPrice').val('');
    $('#addServiceFrm #patientPrice').text('');
    $('#addServiceFrm #originalPrice').text('');
}

function deleteServiceItem(insServiceId) {
	var name	=	$('#serviceName_' + insServiceId).text();
	var message	=	"<span style='color: red; font-weight: bold'>" + name + "</span>";					
	promptConfirm(_('Delete service'), message, function() {
		$.ajax({
			type: 	'POST',
			url:	'../InsurancePayment/deletePaymentItemAjax?set_ajax_view',
			data:	'paymentItemId=' + insServiceId +
			        '&itemListRender=renderItemList' ,
			dataType: 'json',
			beforeSend: function() {
				showAjaxLoader(insServiceId + '_loader', insServiceId + '_btn', true);
			},
			success: function(data) {
				if (data.error.code == 'OK') {
	                setDataToElements({itemList: 'serviceListDiv', paymentSummary: 'paymentSummaryDiv'}, data.result);
	                resetAddServiceItemForm();
	                $('#addServiceFrm #serviceCode').focus();
	            }
	            else {
	                displayError(data.error.mess);
	            }
			}		
		});
		return true;
	},'YesNo', 'warn' ,_('No'));		
} 
